<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="iphone" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree
          :default-expand-all="true"
          :data="list"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="iphone" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length===0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <el-dialog
        :title="isEdit?'编辑部门':'新增子部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        width="50%"
        :before-close="handleClose"
        @closed="hClose"
      >
        <!-- v-if="showDialog"解决表单数据不更新的问题的方式1 -->
        <deptDialog
          :id="curId"
          ref="deptRef"
          :is-edit="isEdit"
          h-edit
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { getDepartment, delDepartment } from '@/api/departments'
import { transDataToTreeData } from '@/utils'
import deptDialog from '@/views/departments/deptDialog.vue'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false, // false 新增||true 编辑
      originList: [] // 保存传给子组件的部门列表
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    async loadDepartment() {
      try {
        // 发送请求
        const res = await getDepartment()
        console.log(res, 'getDepartment')
        // 用户提醒
        if (!res.code === 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        console.log(res.data.depts, 'res.data.depts')
        // 获取部门数据进行处理，然后传递给子组件
        this.originList = res.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        // 删除第一条数据 公司的名称
        res.data.depts.shift()
        // res.data.depts平铺数据=> 使用transDataToTreeData转化为树数据
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 获取id 使用子传父传给新增子级部门作为pid
    hAdd(id) {
      this.isEdit = false
      this.showDialog = true
      this.curId = id
    },
    // 获取id 使用子传父传给编辑子级部门作为pid
    hEdit(id) {
      this.isEdit = true
      this.curId = id
      this.showDialog = true
      // 解决数据不更新的方式2
      // 保证dom更新完成之后，在调用子组件中的方法，重新获取数据，填充表单
      this.$nextTick(() => {
        this.$refs.deptRef.loadDepartsDetail()
      })
    },
    // 删除部门
    hDel(id) {
      this.$confirm('您确定要删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确定实现删除
        const res = await delDepartment(id)
        console.log(res, 'delDepartment')
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.loadDepartment()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hSuccess() {
    // 重新渲染列表 关闭弹层
      this.loadDepartment()
      this.showDialog = false
    },
    hClose() {
      // 重置表单的校验规则和数据
      this.$refs.deptRef.resetForm()
    }
  }
}
</script>
